var cusUnique;//用户唯一性标识
var cusFlag;//客户身份 0-注册用户  1-游客
var cusId=0;

$(document).ready(function() {
	cusUnique=getCookie("cusUnique");
	if(cusUnique!=null){
		loadCusInfo(cusUnique);
	}
});

//加载用户基本信息
function loadCusInfo(cusUnique) {
	$.ajax({
		url : "../cus/findCusById.do",
		type : "post",
		data : {
			"cusUnique" : cusUnique
		},
		dataType : "json",
		success : function(result) {
			console.log(result);
			if (result.status == 1) {// 查询成功
				cusId=result.data.cusId;
				$("#cusAccount").text(result.data.cusAccount);
				$("#cusName").text(result.data.cusName);
				$("#cusPhone").text(result.data.cusPhone);
				$("#cusEmail").text(result.data.cusEmail);
			} else {
				$("#cusAccount").text("游客");
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});
}

//点击修改用户信息
function modifyInfo(){
	$("#McusName").val("");
	$("#McusPhone").val("");
	$("#McusEmail").val("");
	$("#updateCusInfo").show();//显示修改页面
	$("#submit").show();
}

//取消修改信息
function cancealInfo(){
	$("#updateCusInfo").css("display","none");//隐藏修改页面
	$("#submit").css("display","none");
}

//提交修改后的信息
function submitInfo(){
	if(cusId==0){
		return;
	}
	var cusName=$("#McusName").val();
	var cusEmail=$("#McusEmail").val();
	var cusPhone=$("#McusPhone").val();
	var rez= /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;//邮箱的正则表达式
	var rez2=/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;//联系电话的正则表达式
	console.log("cusName:"+cusName+";cusEmail:"+cusEmail+";cusPhone:"+cusPhone);
	var ok=true;
	if(cusEmail==""){
		$("#McusEmail").focus();
		ok=false;
		return;
	}
	if(cusPhone==""){
		$("#McusPhone").focus();
		ok=false;
		return;
	}
	if(cusName==""){
		$("#McusName").focus();
		ok=false;
		return;
	}
	if(rez.test(cusEmail)==false){
		
		$('#McusEmail').val('邮箱格式不正确！');
		
		$("#McusEmail").focus();
		ok=false;
		return;
	}
	if(rez2.test(cusPhone)==false){
		$('#McusPhone').val('电话号码格式不正确！');
		$("#McusPhone").focus();
		ok=false;
		return;
	}
	if(ok){
		$.ajax({
			url : "../cus/updateCusById.do",
			type : "post",
			traditional: true,
			data : {"cusUnique":cusUnique,"cusName":cusName,"cusEmail":cusEmail,"cusPhone":cusPhone},
			dataType : "json",
			success : function(result) {
				
				if (result.status == 1) {// 查询成功
					window.location.href="center.html";
				} else {
					
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}
}


